﻿package net.nicoptere.utils 
{
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author nicolas barradeau
	 */
	public class DictionaryUtil 
	{
		
		public function DictionaryUtil() { }
		
		/**
		* 	returns an Array of all keys within the specified dictionary.   
		* 	@param d The Dictionary instance whose keys will be returned.
		* 	@return Array of keys contained within the Dictionary
		*/                                      
		public static function getKeys(d:Dictionary):Array
		{
				var a:Array = new Array();
				
				for (var key:* in d)
				{
						a.push(key);
				}
				
				return a;
		}
        
		/**
		*  returns an Array of all values within the specified dictionary.
		*  @param d The Dictionary instance whose values will be returned.
		*  @return Array of values contained within the Dictionary
		*/                                      
		public static function getValues(d:Dictionary):Array
		{
			var a:Array = new Array();
			for each (var value:* in d)
			{
				a.push(value);
			}
			return a;
		}

		/**
		*	builds up a Dictionary after an array
		*	@param arr The array from which the dictionary will be built
		*	@return a Dictionary conataining the input array's items
		*/
		public static function fromArray( arr:Array, weakKeys:Boolean = false ):Dictionary
		{
			var dico:Dictionary = new Dictionary( weakKeys );
			var i:int, len:int = arr.length;
			for ( i = 0; i < len; i++ )
			{
				var item:* = arr[ i ];
				dico[ item ] = item;
			}
			return dico;
		}

		/**
		*       interverts two elements of the Dictionary
		*       @param dico The dictionary to swap items from
		*       @param item0 first object to swap
		*       @param item1 second object to swap
		* 		@return the modified dictionary with swapped items
		*/
		public static function swapValues( dico:Dictionary, item0:*, item1:* ):void
		{
			
			var tmp:* = dico[ item0 ];
			dico[ item0 ] = dico[ item1 ];
			dico[ item1 ] = tmp;
			
		}
		
	}
	
}